package com.water.app.Controller.shopping;

import com.water.app.*;
import com.water.app.Exceptions.BalanceException;
import com.water.app.Exceptions.StockException;
import com.water.app.interfaces.account.EmailService;
import com.water.app.interfaces.shopping.ShoppingService;
import com.water.app.mapper.userMapper;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static com.water.app.Constant.THREAD_LOCAL;

@RestController
@RequestMapping("/api/shopping")
public class ShoppingController {
    public static final Logger log= LoggerFactory.getLogger(ShoppingController.class);
    @Autowired
    private ShoppingService shoppingService;
    @Autowired
    private ThreadPoolExecutor poolExecutor;
    @Autowired
    private EmailService emailService;
    @Autowired
    private userMapper userMapper;

    @PostMapping("/intoCart")
    public Result intoCart(@RequestBody cartParam cartParam){
        Integer goodsId=cartParam.getGoodsId();
        Integer number=cartParam.getNumber();
        log.info("goodsId:{},number:{}",goodsId,number);
        if(goodsId==null||number==null||goodsId<=0||number<0){
            log.info("传参错误");
            return Result.error("传参失败");
        }
        int userId= THREAD_LOCAL.get();
        boolean result= shoppingService.insertCart(userId,goodsId,number);
        if(!result){
            log.info("{}加入购物车失败",userId);
            return Result.error("加入购物车失败");
        }
        log.info("加入购物车成功");
        return Result.success("加入购物车成功");
    }

    @PostMapping("/showCart")
    public Result showCart(){
        int userId= THREAD_LOCAL.get();
        log.info("正在展示{}用户的购物",userId);
        List<Goods> list = shoppingService.showCart(userId);
        if(list==null||list.isEmpty()){
            log.info("用户{}的购物车为空",userId);
            return Result.error("用户购物车为空");
        }
        log.info("用户{}查询购物车成功",userId);
        return Result.success(1,"查询购物车成功",list);
    }

    @Autowired
    private RedissonClient redissonClient;

    @PostMapping("/pay")
    public Result pay(@RequestBody goodsListParam goodsListParam) throws BalanceException, StockException, InterruptedException {
        int userId= THREAD_LOCAL.get();
        RLock userLock = redissonClient.getLock("taobao:lock:userId:" + userId);
        try {
            boolean isLock = userLock.tryLock(5, 15, TimeUnit.SECONDS);
            if(!isLock){
                return Result.error("请勿重复点击");
            }
            Integer addressId=goodsListParam.getAddressId();
            List<Integer> goodsIdList=goodsListParam.getGoodsId();
            log.info("goodsIdList:{}",goodsIdList);
            if (goodsIdList==null||goodsIdList.isEmpty()){
                log.info("goodsIdList传参错误");
                return Result.error("传参错误");
            }
            boolean result= shoppingService.pay(goodsIdList,userId,addressId);
            if(!result){
                log.info("支付失败");
                return Result.error("支付失败");
            }
            log.info("支付成功");
            return Result.success("支付成功");
        } catch (InterruptedException | StockException | BalanceException e) {
            throw e;
        } finally {
            if(userLock.isHeldByCurrentThread()) {
                userLock.unlock();
            }
        }
    }




    @GetMapping("/skill")
    public Result skill(int goodsId){
        return shoppingService.sKill(goodsId);
    }

}
